getline — Read scalars and text
The getline
function is used to read in a line of text from the
file specified in the argument, and return that line as a list. Each
time you call getline
,
another line is read from the file. To get back to the start of the
file, you have to use close
. getline
uses spaces and
tabs as delimiters of words. The first word on the line becomes the
first element in the list, the second word on the line becomes the
second element, and so on. getline
is smart enough to
recognise numbers and strings, and can return either or both as
elements in the list. Numbers are recognised in normal or in exponential
notation. You can get around this by putting the number in
"
-type quotation marks. Numbers are not recognised in complex
notation. A list is always returned — if the line was empty, you
an empty list is returned.
The argument can also be used to specify a process to run instead of a
file to read from. The main limitation is that the process has to be
able to write to standard output, which most tools can do. To set this
up, just make |
the first character of the filename. You can
get even more flexibility by using a filter on the original process.
A common operation is to read the whole file. Since getline
returns an empty list when there is no input, we can tell when to
terminate the input loop by checking for a zero length result.
Subsections